<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDele(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->

              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryParams.page"
                :page-sizes="[1, 3, 4, 6, 300]"
                :page-size="queryParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      
      <!-- 新增弹框 -->
<el-dialog title="编辑弹层" @close="hResetsFrom"
   :visible.sync="showDialog">
  <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
    <el-form-item label="角色名称" prop="name">
      <el-input v-model="roleForm.name" />
    </el-form-item>
    <el-form-item label="角色描述" prop="description">
      <el-input v-model="roleForm.description" />
    </el-form-item>
  </el-form>
  <!-- 底部 -->
  <el-row slot="footer" type="flex" justify="center">
    <el-col :span="6">
      <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="showDialog=false">取消</el-button>
    </el-col>
  </el-row>
</el-dialog>
    </div>
  </div>
</template>

<script>
import { getRolesList ,addRolesList,updateRolesList,deleRolesList} from "@/api/settings";
export default {
  data() {
    return {
      roleForm:{
        name:'',
        description:''
      },
      rules:{
        name:[
          { required: true, message: '请输入角色名称', trigger: 'blur' },
        ],
        description:[
          { required: true, message: '请输入角色描述', trigger: 'blur' },
        ],
      },
      // 显示隐藏
      showDialog: false,
      // 分页数
      queryParams: {
        page: 1, //页码值 默认第一页
        pagesize: 4, //每页显示条数
      },
      // 角色列表数据
      list: [],
      // 保存角色总数
      total: 0,
      isEdit:false
    };
  },
  // 生命钩子
  created() {
    this.loadRolesList();
  },
  methods: {
    // 重置
    hResetsFrom(){
      this.$refs.roleForm.resetFields()
      this.roleForm={
        name:'',
        description:''
      }
    },
    // 删除
    hDele(id){
      console.log(id);
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
           try {
        const res = await deleRolesList(id)
        console.log(res);
        if(this.list.length===1){
          this.queryParams.page--
         if(this.queryParams.page<=0){
          this.queryParams.page=1
         }
        }
        this.loadRolesList()
      } catch (error) {
        console.log(error);
      }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });  
    },
    // 编辑角色
   async doEdit(){
      try {
        await updateRolesList(this.roleForm)
        this.loadRolesList()
        this.showDialog=false
      } catch (error) {
        console.log(error);
      }
    },
    // 编辑
    hEdit(data){
      this.isEdit=true
      // 因为this.roleForm进行了表单的双向绑定，那么表单中的数据改变, this.roleForm也会发送改变
      // 而this.roleForm和传进来的data(表格的数据对象)，所以导致，表单的数据改变,表格中的数据跟着改变解决的办法:使用浅拷贝
      this.roleForm={...data}
      this.showDialog=true
    },
    // 新增角色
    hAdd(){
      this.isEdit=false
      this.showDialog=true
    },
    // 新增角色
   async doAdd(){
       try {
          const res = await addRolesList(this.roleForm)
          // console.log(res);
          this.loadRolesList()
          this.showDialog= false
        } catch (error) {
          console.log(error);
        }
    },
    // 确定按钮
    hSubmit(){
      this.$refs.roleForm.validate((valid)=>{
        if(!valid) return
        // 新增角色
        this.total++//新增成功 后台总数加1 所以先计算最大页码值先加
        // 将queryParams.page 改变到最后一页 再从新渲染
        // 计算最大页码值
        const mathPage = Math.ceil(this.total / this.queryParams.pagesize)
       
        if(this.isEdit){
          this.doEdit()
        }else{
          this.queryParams.page= mathPage
          this.doAdd() 
        }        
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.queryParams.page = 1;
      this.queryParams.pagesize = val;
      this.loadRolesList();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.queryParams.page = val;
      this.loadRolesList();
    },
    // 获取所有角色列表
    async loadRolesList() {
      try {
        const res = await getRolesList(this.queryParams);
        console.log(res);
        this.list = res.data.rows;
        this.total = res.data.total;
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style>
</style>